package com.sunlg.smarthome.contract.presenter;

import com.sunlg.smarthome.base.App;
import com.sunlg.smarthome.base.C;
import com.sunlg.smarthome.contract.FireWarningContract;
import com.sunlg.smarthome.data.entity.FireWarning;
import com.sunlg.smarthome.data.remote.MySubscriber;
import com.sunlg.smarthome.data.remote.Result;
import com.sunlg.smarthome.data.remote.SHServiceWrapper;
import com.sunlg.smarthome.utils.PreferHelper;

import java.util.List;

/**
 * Created by sunlg on 2017/5/29.
 */

public class FireWarningPresenter implements FireWarningContract.Presenter{

    private FireWarningContract.View mView;
    private PreferHelper preferHelper;
    private SHServiceWrapper mSHServiceWrapper;

    public FireWarningPresenter() {
        preferHelper = new PreferHelper(App.getInstance(), "gateway");
        mSHServiceWrapper = App.getInstance().getServiceWrapper();
    }

    @Override
    public void loadAllFireWarning() {
        mSHServiceWrapper.getAllFireWarning(preferHelper.getInt("id", 0),
                new MySubscriber<Result<List<FireWarning>>>() {
                    @Override
                    public void onSuccess(Result<List<FireWarning>> data) {
                        if(data.getCode() == C.ActionStatus.SUCCESS){
                            mView.loadFireWarningSuccess(data.getData());
                        }
                    }

                    @Override
                    public void _onError(Throwable e) {
                        mView.showError(e.getMessage());
                    }
                });
    }

    @Override
    public void attachView(FireWarningContract.View view) {
        mView = view;
    }

    @Override
    public void dettachView() {
        mView = null;
    }
}
